package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.DishAndFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/setmeal")
@ResponseBody
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @PostMapping
    public R saveSetmeal(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealAndSetmealDish(request,setmealDto);
        return R.success("保存成功");
    }
    @GetMapping("page")
    public R pageDish(int page, int pageSize, String name) {
        return setmealService.pageDish(page, pageSize, name);
    }
    @DeleteMapping
    public R deleteDish(Long[] ids){
        setmealService.deleteDish(ids);
        return R.success("删除成功");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealById(@PathVariable("id") Long id){
        return R.success(setmealService.getSetmealById(id));
    }
    @PutMapping()
    public R updateSetmeal(HttpServletRequest request, @RequestBody SetmealDto setmealDto) {
        try {
            setmealService.updateSetmeal(request, setmealDto);
        } catch (Exception e) {
            return R.error("已有同名套餐存在");
        }
        return R.success("修改成功");
    }
    @PostMapping("/status/{status}")
    public R updateStatusById(@PathVariable("status") Integer status,Long[] ids){
        setmealService.updateStatusById(status,ids);
        return R.success("更新售卖状态成功");
    }
    //?categoryId=1413342269393674242&status=1
    @GetMapping("/list")
    public R setmealList(Long  categoryId ,Integer status){
        return R.success(setmealService.selectListByCaGorId(categoryId,status));
    }
}
